package com.boarsoft.boar.batch.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.boar.batch.BatchContext;
import com.boarsoft.boar.batch.service.BatchDispatcher;

/**
 * 用于定时调起流程的Quartz Job子类<br/>
 * 
 */
public class SimpleBatchQuartzJob implements Job {
	private static final Logger log = LoggerFactory.getLogger(SimpleBatchQuartzJob.class);

	/**
	 * 调起指定的流程
	 */
	@Override
	public final void execute(JobExecutionContext jec) throws JobExecutionException {
		// JobDataMap jdm = jec.getJobDetail().getJobDataMap();
		// String infoId = jdm.getString("infoId");
		// String catalog = jdm.getString("catalog");
		// String code = jdm.getString("code");
		// String impl = jdm.getString("impl"); // dispatcher
		// String params = jdm.getString("params");
		// log.info("Trigger batch {}/{} with dispatcher {} and params: {}", //
		// catalog, code, impl, params);
		JobKey jk = jec.getJobDetail().getKey();
		String impl = jk.getGroup(); // BatchInfo.impl / dispatcher
		String id = jk.getName(); // BatchInfo.id
		try {
			BatchDispatcher bd = BatchContext.getDispatcher(impl);
			if (bd == null) {
				log.warn("Can not find batch dispatcher {}", impl);
				return;
			}
			log.info("Trigger batch {} with dispatcher {}", id, impl);
			bd.start(id);
		} catch (Throwable e) {
			log.error("Error on invoke batch {}", id, e);
		}
	}
}
